"use strict";

import { log } from "@p-test-cli/utils";

const colors = require("colors/safe");
const semver = require("semver");
const LOWEST_NODE_VERSION = "18.0.0";
export class Command {
  _argv;
  _cmd;
  constructor(argv) {
    if (!argv) {
      throw new Error("参数不能为空！");
    }
    if (!Array.isArray(argv)) {
      throw new Error("参数对象必须为数组！");
    }
    if (argv.length < 1) {
      throw new Error("参数列表不能为空！");
    }
    this._argv = argv;

    let runner = new Promise((resolve, reject) => {
      let chain = Promise.resolve();
      chain = chain.then(() => this.checkNodeVersion());
      chain = chain.then(() => this.initArgs());
      chain = chain.then(() => this.init());
      chain = chain.then(() => this.exec());
      chain.catch((err) => {
        log.error("error", err.message);
      });
    });
  }
  initArgs() {
    this._cmd = this._argv[this._argv.length - 1];
    this._argv = this._argv.slice(0, this._argv.length - 1);
  }
  checkNodeVersion() {
    const curVersion = process.version;
    const lowerstVersion = LOWEST_NODE_VERSION;
    if (!semver.gte(curVersion, lowerstVersion)) {
      throw new Error(
        colors.red(`p-test-cli 需要安装 v${lowerstVersion} 以上版本的node.js`)
      );
    }
  }
  init() {
    throw new Error("init必须实现");
  }
  exec() {
    throw new Error("exec必须实现");
  }
}
